﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;

namespace Maticsoft222.DBUtility
{
    public class TreeHelper
    {
        public List<TreeNode> GetAllNodes(DataTable source, string codeField, string textField)
        {
            List<TreeNode> allNodes = new List<TreeNode>();
            foreach (DataRow rowTmp in source.Rows)
            {
                TreeNode nodeTemp = new TreeNode();
                nodeTemp.Value = rowTmp[codeField].ToString();
                nodeTemp.Text = rowTmp[textField].ToString();
                nodeTemp.NavigateUrl = "?id=" + rowTmp[codeField].ToString();
                allNodes.Add(nodeTemp);
            }
            return allNodes;
        }

        //构成树////////////////////////////
        //找到父亲节点
        private TreeNode GetFatherNode(string currentName, List<TreeNode> allNodes)
        {
            int count = currentName.Length;
            while (count >= 1)
            {
                string fatherName = currentName.Substring(0, count - 1);
                for (int i = 0; i < allNodes.Count; i++)
                {
                    if (allNodes[i].Value == fatherName)
                    {
                        return allNodes[i];//out for 
                    }
                }
                //计算器减一
                count -= 1;
            }//end while 
            return null;
        }

        //把孩子节点和父亲节点连起来
        private void ConnectFatherAndChild(TreeNode child, TreeNode father)
        {
            father.ChildNodes.Add(child);
        }

        //生成树结构
        public void CreateTree(List<TreeNode> allNodes)
        {
            foreach (TreeNode nodeTmp in allNodes)
            {
                //如果有爸爸
                if (GetFatherNode(nodeTmp.Value, allNodes) != null)
                {
                    TreeNode fatherTmp = GetFatherNode(nodeTmp.Value, allNodes);
                    ConnectFatherAndChild(nodeTmp, fatherTmp);
                }
            }//end  foreach
        }

        //得到根节点,可能有多个根节点
        public List<TreeNode> GetRootNodeList(List<TreeNode> allNode)
        {
            List<TreeNode> rootList = new List<TreeNode>();
            for (int i = 0; i < allNode.Count; i++)
            {
                //没有爸爸就是根节点
                if (allNode[i].Parent == null)
                {
                    rootList.Add(allNode[i]);
                }
            }//end for 
            return rootList;
        }

        //把根节点绑定到treeview控件
        public void BindTreeViewWithRoots(TreeView view, List<TreeNode> roots)
        {
            foreach (TreeNode nodeTmp in roots)
            {
                view.Nodes.Add(nodeTmp);
            }
        }

        //全自动绑定treeview控件
        public void AutoTreeView(TreeView view, List<TreeNode> allNodes)
        {
            CreateTree(allNodes);
            List<TreeNode> roots = GetRootNodeList(allNodes);
            BindTreeViewWithRoots(view, roots);
        }
        /////////////////////////////////////////////



    }
}
